'use strict';

const ulid = require('../public/ulid');

const Service = require('egg').Service;

class ScriptService extends Service {
  async list(params) {
    const tid = params.tid;
    return await this.ctx.modelmysql.Script.findAndCountAll({
      where: {
        tid,
        olttypeId: params.oltTypeId,
      },
      offset: parseInt((params.page - 1) * params.limit),
      limit: parseInt(params.limit),
    });
  }
  async create(params) {
    const tid = params.tid;
    await this.ctx.modelmysql.Script.create({
      id: ulid.ulid(),
      name: params.name,
      script: params.script,
      tid,
      version: params.version,
      MountPoint: params.MountPoint,
      olttypeId: params.oltTypeId,
    });
  }
  async edit(params) {
    await this.ctx.modelmysql.Script.update({
      script: params.script,
      version: params.version,
      MountPoint: params.MountPoint,
    },
    {
      where: { id: params.id },
    });
  }
  async delete(params) {
    await this.ctx.modelmysql.Script.destroy({ where: { id: params.id } });
  }
  async singleScript(params) {
    return await this.ctx.modelmysql.Script.findOne({ where: { id: params.id } });
  }
}

module.exports = ScriptService;
